package io.mft.commons.db.mapper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

import cn.hutool.core.util.ObjectUtil;
import io.mft.commons.db.mapper.annotation.Mapper;
import io.mft.commons.db.mapper.cache.MapperMetadataCache;
import io.mft.commons.ioc.ApiBeanName;
import io.mft.commons.ioc.BaseRegistry;
import io.mft.commons.ioc.SpringIocRegistry;

/**
 * 
 * @类型名称：MapperSpringRegistry
 * @说明：TODO
 * @创建者: 胡常建
 * @创建时间: 2018年3月7日 下午11:54:33
 * @修改者: 胡常建
 * @修改时间: 2018年3月7日 下午11:54:33
 */
@SpringIocRegistry
public class MapperSpringRegistry extends BaseRegistry{
	private static final Set<String> mapperClassTypes=new HashSet<String>();
	public MapperSpringRegistry(String sysNo, String basePackage) {
		super(sysNo, basePackage, Mapper.class);
	}
	
	@Override
	protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
		return beanDefinition.getMetadata().isInterface();
	}

	@Override
	protected boolean handle(AbstractBeanDefinition genericBeanDefinition) throws Exception{
		genericBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(genericBeanDefinition.getBeanClassName()); // issue #59
		genericBeanDefinition.setBeanClass(MapperFactory.class);
		return true;
	}
	
	protected  List<ApiBeanName> parseBeanName(String annotationName,String sysNo,BeanDefinition definition){
		List<ApiBeanName> list=new ArrayList<ApiBeanName>();
		AnnotatedBeanDefinition annotatedBeanDefinition=(AnnotatedBeanDefinition)definition;
		Map<String, Object> map=annotatedBeanDefinition.getMetadata().getAnnotationAttributes(annotationName);
		Object value=map.get("value");
		if (value!=null&&!ObjectUtil.isEmpty(String.valueOf(value))) {
			String beanName =sysNo+"."+String.valueOf(value);
			ApiBeanName api_beanName=new ApiBeanName();
			api_beanName.setBeanName(beanName);
			api_beanName.setQualifiedName(definition.getBeanClassName());
			api_beanName.setApiInterface(false);
			api_beanName.setAnnotationType(annotationName);
			api_beanName.setSysNo(sysNo);
			api_beanName.setBeanClassName(definition.getBeanClassName());
			list.add(api_beanName);
		}else {
			String api=definition.getBeanClassName();
			String[] api_arr=api.split("\\.");
			String beanName =sysNo+"."+api_arr[api_arr.length-1];
			ApiBeanName api_beanName=new ApiBeanName();
			api_beanName.setBeanName(beanName);
			api_beanName.setQualifiedName(api);
			api_beanName.setApiInterface(true);
			api_beanName.setAnnotationType(annotationName);
			api_beanName.setSysNo(sysNo);
			api_beanName.setBeanClassName(definition.getBeanClassName());
			list.add(api_beanName);
		}
		return list;
	}

	public void reg(BeanDefinitionRegistry registry){
		try {
			List<ApiBeanName> apiBeanNames=load(registry);
			for (ApiBeanName apiBeanName : apiBeanNames) {
				boolean s=mapperClassTypes.add(apiBeanName.getBeanClassName());
				if (s) {
					Class<?> classType =Thread.currentThread().getContextClassLoader().loadClass(apiBeanName.getBeanClassName());
					//logger.info("初始化[" + mapperClassType + "]Mapper元数据开始,put解析到缓存");
					MapperMetadataCache.getInstance().put(classType);
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}
}
